<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller {

    /**
     * file :tp的验证码   超级简单方面     会生成验证码和时间戳存放在session
     *     格式         [d2d977c58444271d9c780187e93f80e5] => Array
    (
     *                                                   [verify_code] => 1bc5fbf842ad76e8dbe1e5f57ff293b1
    [verify_time] => 1476414923
    )

     */
    public function  createCode(){
        $config =    array(
            'expire'      =>    1000,   //验证码过期时间
            'useCurve'    =>    false,//曲线
            'fontSize'    =>    40,    // 验证码字体大小
            'length'      =>    4,     // 验证码位数
            'useNoise'    =>    false, // 关闭验证码杂点
        );
        $Verify = new \Think\Verify($config);
        $Verify->codeSet = '0123456789';
        $Verify->entry();
    }
    /**
     * file :tp自带验证码功能
     * time:2016/10/11
     * author:lin
     */
    public function index(){
        $Verify = new \Think\Verify();
        //   $Verify->entry();
        $this->display();
       // print_r($_SESSION['verify_code']);
    }
    /**
     *  file :登录操作  根据登录类型定位不同的登录方法     账号  密码 appid 保存session
     *  time:2016/10/11
     *  author:lin
     *  post : user_id     password   type
     *  tp自带验证码功能
     */
    public function  login(){

        //  返回状态码:result['Rt_Code'] ->1:登录成功    0:登录失败:输入为空或者账号密码错误     -1:验证码错误
        //  result['Rt_Msg']->登录结果描述
        //json 返回

        //初始化返回结果数组
        $result = '';
        $user_id   = I('post.user_id');
        $pass_word = I('post.password');
        //登录类型  1:管理员  2:教师 3:学生
        $type      = I('post.type');
        //验证码
        $code      = I('post.code');

        //空判断
        if(empty($user_id)||empty($pass_word)||empty($type)){
            $result['Rt_Code'] = 0 ;
            $result['Rt_Msg']  = "提交的参数不能为空";
            $this->ajaxReturn($result);
            exit();
        }
        //判断验证码
        if(!($this->check_verify($code))){
            $result['Rt_Code'] = -1 ;
            $result['Rt_Msg']  = "验证码错误";
            $this->ajaxReturn($result);
            exit();
        }
        switch ($type){
            case 'admin':
                $result = $this->adminLogin($user_id,$pass_word,$type);
                break;
            case 'teacher':
                $result = $this->teacherLogin($user_id,$pass_word,$type);
                break;
            case 'student':
                $result = $this->studentLogin($user_id,$pass_word,$type);
                break;
            default:
                break;
        }
        //ajax   状态返回前端
        $this->ajaxReturn($result);

    }
    /**
     *  file :管理员登录
     *  time:2016/10/11
     *  author:lin
     *
     */
    public function  adminLogin($user_id,$password,$type){
        $result = $this->login_check($user_id,$password,$type,'admin');
        return $result;

    }
    /**
     *  file:教师登录
     *  time:2016/10/11
     *  author:lin
     */
    public function  teacherLogin($user_id,$password,$type){
        $result = $this->login_check($user_id,$password,$type,'teacher');
        return $result;

    }
    /**
     * file:学生登录
     * time:2016/10/11
     * author:lin
     */
    public function  studentLogin($user_id,$password,$type){
        $result = $this->login_check($user_id,$password,$type,'student');
        return $result;

    }

    /**
     * file: 主要验证函数 和session保存
     * @param $table_name 表明
     * @param $user_id     账号
     * @param $password    密码
     * @return result    结果数组  状态码和说明
     * @time :2016/10/11
     * @author: lin
     *
     *
     */
    public function  login_check($user_id,$password,$type,$table_name){
        $login_Obj = M($table_name);
        $passwd    = $login_Obj->where('user_id='.$user_id)->getField('password');
        if($password != $passwd){
            $result['Rt_Code'] = -1;
            $result['Rt_Msg']  = '密码错误';
            return $result;
        }
        //登录成功  保存账号和自定义appid  控制页面访问     在session 保存    格式:admin(前缀)+$user_id(账号)+$num(随机数)
        $_SESSION['user_id'] = $user_id;
        $_SESSION['type']    =$type;
//        $num                 = $this->luckNum();
        switch ($table_name){
            case 'admin':
                $_SESSION['appid'] = 'admin_'.$user_id;
                break;
            case 'teacher':
                $_SESSION['appid'] = 'teacher_'.$user_id;
                break;
            case 'student':
                $_SESSION['appid'] = 'student'.$user_id;
                break;
            default:
                break;
        }

        $result['Rt_Code'] = 1;
        $result['Rt_Msg']  = '登录成功';
        return $result;

    }

    /**
     * file:生成随机数
     * @return int
     * @time :2016/10/11
     * @authoor: lin
     */
    private function  luckNum(){
        $luck_num = rand(1000,9999);
        return $luck_num;
    }

    /**
     * file:检验验证码
     * @param $code :用户输入的验证码
     * @param $id :默认为空  多个验证码用1 2 3。。。
     * @return 正确 true   false
     */
     private  function check_verify($code, $id = ''){
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }
}